unit unitSaveSettings;

interface

uses Windows, SysUtils, IniFiles;

procedure LoadSettingsFromIni;
procedure LoadVisualsFromSettings;
procedure SaveVisualsToIni;

implementation

uses Unit1;

procedure LoadSettingsFromIni;
var
  Ini: TINIFile;
begin
  Ini := TINIFile.Create(Form1.Dir + 'settings.ini');
  Form1.ConnPort := Ini.ReadInteger('common', 'connport', 606);
  Form1.asConnAddress := Ini.ReadString('common', 'connaddress', '127.0.0.1');
  Form1.bUseEvilCompression := (Ini.ReadString('common', 'compression', 'false')
    = 'true');
  Form1.bUseEvilCompression_CPU := Form1.bUseEvilCompression;

  Form1.bUseIPTOFU := (Ini.ReadString('common', 'iptofumd5', 'false') = 'true');
  Form1.bUseTORTRUST := (Ini.ReadString('common', 'tortrust', 'false')
    = 'true');

  Form1.bExtThreadsVisualization :=
    (Ini.ReadString('visuals', 'extthreads', 'false') = 'true');
  Form1.dwShowTime := Ini.ReadInteger('visuals', 'showtime', 1000);
  Form1.dwSocketsTimeOut := Ini.ReadInteger('common', 'timeout', 1000);

  Form1.asTorIP := Ini.ReadString('common', 'torserver', '127.0.0.1');
  Form1.wTorPort := Ini.ReadInteger('common', 'torport', 9050);

  Form1.iMaxCPUUsage := Ini.ReadInteger('common', 'maxcpuusage', 100);

  Ini.Free;
end;

procedure LoadVisualsFromSettings;
begin
  {
    Form3.Edit1.Text := IntToStr(Form1.ConnPort);
    Form3.CheckBox1.Checked := Form1.bExtThreadsVisualization;
    Form3.CheckBox3.Checked := Form1.bUseEvilCompression;
    Form3.CheckBox4.Checked := Form1.bUseIPTOFU;
    Form3.CheckBox5.Checked := Form1.bUseTORTRUST;
  }
  Form1.PortToConnectTo.Text := IntToStr(Form1.ConnPort);
  Form1.IsThreadsVisualization.Checked := Form1.bExtThreadsVisualization;
  Form1.IsUseDataCompression.Checked := Form1.bUseEvilCompression;
  Form1.IsUseTOFU.Checked := Form1.bUseIPTOFU;
  Form1.IsTORTrust.Checked := Form1.bUseTORTRUST;

  {
    Form3.Edit3.Enabled := Form3.CheckBox1.Checked;
    Form3.Edit5.Enabled := Form3.CheckBox5.Checked;
    Form3.Edit6.Enabled := Form3.CheckBox5.Checked;
    Form3.Edit7.Enabled := Form3.CheckBox3.Checked;
  }
  Form1.ThreadShowTime.Enabled := Form1.IsThreadsVisualization.Checked;
  Form1.RibbonGroup7.Enabled := Form1.IsTORTrust.Checked;
  Form1.CPUUsageLimit.Enabled := Form1.IsUseDataCompression.Checked;

  {
    Form3.Edit4.Text := IntToStr(Form1.dwSocketsTimeOut);
    Form3.Edit3.Text := IntToStr(Form1.dwShowTime);
    Form3.Edit7.Text := IntToStr(Form1.iMaxCPUUsage);
  }

  Form1.TimeOut.Text := IntToStr(Form1.dwSocketsTimeOut);
  Form1.ThreadShowTime.Text := IntToStr(Form1.dwShowTime);
  Form1.CPUUsageLimit.Text := IntToStr(Form1.iMaxCPUUsage);

  {
    if Form1.asConnAddress = '0' then
    Form3.CheckBox2.Checked := true
    else
    begin
    Form3.CheckBox2.Checked := false;
    Form3.Edit2.Text := Form1.asConnAddress;
    end;
  }

  if Form1.asConnAddress = '0' then
  begin
    Form1.IsAcceptAnyAddress.Checked := true;
    Form1.AddressToAccept.Enabled := true;
  end
  else
  begin
    Form1.IsAcceptAnyAddress.Checked := false;
    Form1.AddressToAccept.Text := Form1.asConnAddress;
    Form1.AddressToAccept.Enabled := false;
  end;
end;

procedure SaveVisualsToIni;
var
  Ini: TINIFile;
begin
  {
    if StrToInt(Edit3.Text) < 1000 then
    Edit3.Text := '1000';

    if (StrToInt(Edit7.Text) > 100) or (StrToInt(Edit7.Text) < 0) then
    Edit7.Text := '100';
  }

  if StrToInt(Form1.ThreadShowTime.Text) < 1000 then
    Form1.ThreadShowTime.Text := '1000';

  if (StrToInt(Form1.CPUUsageLimit.Text) > 100) or
    (StrToInt(Form1.CPUUsageLimit.Text) < 0) then
    Form1.CPUUsageLimit.Text := '100';

  {
    Ini := TIniFile.Create(Form1.Dir + 'settings.ini');
    Ini.WriteInteger('common', 'connport', StrToInt(Edit1.Text));

    Ini.WriteInteger('common', 'timeout', StrToInt(Edit4.Text));

    if CheckBox2.Checked then
    Ini.WriteString('common', 'connaddress', '0')
    else
    Ini.WriteString('common', 'connaddress', Edit2.Text);
  }

  Ini := TINIFile.Create(Form1.Dir + 'settings.ini');
  Ini.WriteInteger('common', 'connport', StrToInt(Form1.PortToConnectTo.Text));

  Ini.WriteInteger('common', 'timeout', StrToInt(Form1.TimeOut.Text));

  if Form1.IsAcceptAnyAddress.Checked then
    Ini.WriteString('common', 'connaddress', '0')
  else
    Ini.WriteString('common', 'connaddress', Form1.AddressToAccept.Text);

  {
    if CheckBox3.Checked then
    Ini.WriteString('common', 'compression', 'true')
    else
    Ini.WriteString('common', 'compression', 'false');

    if CheckBox4.Checked then
    Ini.WriteString('common', 'iptofumd5', 'true')
    else
    Ini.WriteString('common', 'iptofumd5', 'false');

    if CheckBox5.Checked then
    Ini.WriteString('common', 'tortrust', 'true')
    else
    Ini.WriteString('common', 'tortrust', 'false');

    Ini.WriteInteger('visuals', 'showtime', StrToInt(Edit3.Text));
  }
  if Form1.IsUseDataCompression.Checked then
    Ini.WriteString('common', 'compression', 'true')
  else
    Ini.WriteString('common', 'compression', 'false');

  if Form1.IsUseTOFU.Checked then
    Ini.WriteString('common', 'iptofumd5', 'true')
  else
    Ini.WriteString('common', 'iptofumd5', 'false');

  if Form1.IsTORTrust.Checked then
    Ini.WriteString('common', 'tortrust', 'true')
  else
    Ini.WriteString('common', 'tortrust', 'false');

  Ini.WriteInteger('visuals', 'showtime', StrToInt(Form1.ThreadShowTime.Text));

  {
    if CheckBox1.Checked then
    Ini.WriteString('visuals', 'extthreads', 'true')
    else
    Ini.WriteString('visuals', 'extthreads', 'false');

    Ini.WriteString('common', 'torserver', Edit5.Text);
    Ini.WriteInteger('common', 'torport', StrToInt(Edit6.Text));

    Ini.WriteInteger('common', 'maxcpuusage', StrToInt(Edit7.Text));
  }

  if Form1.IsThreadsVisualization.Checked then
    Ini.WriteString('visuals', 'extthreads', 'true')
  else
    Ini.WriteString('visuals', 'extthreads', 'false');

  Ini.WriteString('common', 'torserver', Form1.TOR_IP.Text);
  Ini.WriteInteger('common', 'torport', StrToInt(Form1.TOR_SOCKS4_PORT.Text));

  Ini.WriteInteger('common', 'maxcpuusage', StrToInt(Form1.CPUUsageLimit.Text));

  Ini.Free;
end;

end.
